/* SDL_entry_windows.cpp
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Entry point for a Windows SDL application.

Unlike the usual SDL entry point, this does not have support for:
-Printing errors to file
-Windows CE

On the other hand, the lack of all the compiler switches for Windows CE and for
error reporting modes makes this much cleaner code. The old entry point code can
be used for any project requiring the things this file leaves out.
*/

#if defined(__WIN32__) || defined(WIN32)

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include "SDL.h"

// SDL_main.h defines "main" as "SDL_main". Clear that definition for this file.
#undef main



// Forward declarations of command line parsing functions.

// Divide the given string into a set of arguments separated by '\0'.
// Return the total number of arguments.
static int ParseArguments(const char *in, char *out);
// Take a string of command line arguments, and construct an argv array.
static void MakeArgV(char *arguments, int argc, char **argv);



// console_main:
// This is where execution begins for console apps.
int console_main(int argc, char *argv[])
{
	// Load SDL dynamic link library.
	if(SDL_Init(SDL_INIT_NOPARACHUTE) < 0)
		return 1;

	SDL_SetModuleHandle(GetModuleHandle(NULL));

	// Run the application main() code.
	int status = SDL_main(argc, argv);

	SDL_Quit();

	return status;
}



// WinMain:
// This is where execution begins for windowed apps.
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
{
	// Make a copy of the command line string.
	char *commandLine = GetCommandLine();
	size_t commandLength = SDL_strlen(commandLine) + 1;
	char *arguments = SDL_stack_alloc(char, commandLength);
	if(!arguments)
		return 1;

	// Parse the command line arguments into argv and argc.
	// Store a copy of the arguments.
	int argc = ParseArguments(commandLine, arguments);
	// Allocate enough space for all the argv pointers.
	char **argv = SDL_stack_alloc(char *, argc + 1);
	if(!argv)
		return 1;
	MakeArgV(arguments, argc, argv);

	// Run the main program (after a little SDL initialization).
	return console_main(argc, argv);
}



// ParseArguments:
// Parse a command line string and reduce it to the form:
// "<arg 0>\0<arg 1>\0...<arg n>\0". That is, a list of arguments
// each separated from the next by a '\0', with no extra whitespace.
int ParseArguments(const char *in, char *out)
{
	int argc = 0;

	while(*in)
	{
		// Skip white space in between arguments.
		if(isspace(*in))
			++in;
		else
		{
			// The next character is part of an argument. Switch depending on whether or not
			// the argument is enclosed in quotation marks.
			if(*in == '"')
			{
				// Skip the opening quotation mark.
				++in;

				while(*in && *in != '"')
				{
					// Handle escape characters. Copy slashes unless they are followed by
					// a quotation mark.
					if(*in == '\\')
					{
						if(*++in == '"')
						{
							// Skip escaped quotation marks.
							*out++ = '"';
							++in;
						}
						else
							*out++ = '\\';
					}
					else
					{
						// Copy the argument until an unescaped quotation mark is reached,
						// or until the end of the input string.
						*out++ = *in++;
					}
				}

				// Skip the closing quotation mark.
				if(*in)
					++in;
			}
			else
			{
				// Copy the argument until the next space or the end of the input string.
				while(*in && !isspace(*in))
					*out++ = *in++;
			}

			// Terminate the argument with a '\0'.
			++argc;
			*out++ = '\0';
		}
	}

	// Return the total number of arguments.
	return argc;
}



// MakeArgV:
// Load the output of ParseArguments() into an argv array.
void MakeArgV(char *arguments, int argc, char **argv)
{
	// Find the start of each argument in the given string.
	char **it = argv;
	char **end = it + argc;
	for( ; it != end; ++it)
	{
		*it = arguments;
		while(*arguments)
			++arguments;
		// We're now at the '\0' terminating this argument.
		++arguments;
	}

	// The last entry in argv should be null.
	*it = NULL;
}



#endif // __WIN32__



/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */
